<template>
  <div class="common-layout">
    <el-container>
      <el-header style="background-color: #fff; height: 180px">
        <div
          class="common-layout-header"
          style="display: flex; flex-direction: column; align-items: flex-start"
        >
          <div class="common-layout-header-title">
            <img src="@/assets/img/列表.png" alt="" />作业管理
          </div>
          <div style="margin-top: 20px">
            <div class="date-input">
              日期：
              <input type="date" v-model="selectedDate" />
            </div>
            <div class="class-info">
              <span>章节：{{ z }}</span>
              <span>小节：{{ x }}</span>
            </div>
            <div class="student-stats">
              <span>班级人数：{{ totalStudents }}</span>
              <span>已交人数：{{ submittedStudents }}</span>
              <span
                class="clickable"
                style="font-size: 19px; cursor: pointer"
                @click="showUnsubmittedDialog = true"
                v-b-tooltip.hover
                title="点击显示未提交的名单"
              >
                未交人数：{{ unsubmittedStudents }}
                <span style="color: #409eff">(点击查看)</span>
              </span>
            </div>
          </div>
        </div>
      </el-header>
      <el-main class="common-layout-main" style="width: 100%">
        <el-table
          :data="filteredTaskList"
          border
          style="width: 100%"
          :header-cell-style="{
            textAlign: 'center',
            backgroundColor: '#f0f9eb',
            padding: '8px',
          }"
          :cell-style="{ textAlign: 'center', padding: '8px' }"
        >
          <!-- 姓名列 -->
          <el-table-column prop="nickName" label="姓名" width="200" />

          <!-- 作业评分列 -->
          <el-table-column prop="work" label="作业评分" width="250" />

          <!-- 评语列，支持文本溢出显示 -->
          <el-table-column
            prop="appraise"
            label="评语"
            show-overflow-tooltip
            width="650"
          />

          <!-- 批改状态列 -->
          <el-table-column prop="approvalStatus" label="批改状态" width="280">
            <template #default="scope">
              <span :style="{ color: opt[scope.row.approvalStatus].color }">
                {{ opt[scope.row.approvalStatus].icon }}
                {{ opt[scope.row.approvalStatus].label }}
              </span>
            </template>
          </el-table-column>
          <!-- 操作列 -->
          <el-table-column prop="operate" label="操作" width="227">
            <template #default="scope">
              <el-button
                @click="correct(scope.row.userId)"
                type="primary"
                link
                icon="Edit"
                style="padding: 6px 12px; border-radius: 4px"
              >
                批改作业
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
    </el-container>

    <!-- 未提交作业的学生 -->
    <el-dialog
      title="未提交作业详情"
      v-model="showUnsubmittedDialog"
      width="50%"
      custom-class="custom-dialog"
    >
      <el-table :data="unsubmittedStudents01" style="width: 100%" stripe border>
        <el-table-column
          prop="nickName"
          label="姓名"
          align="center"
        ></el-table-column>
      </el-table>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="showUnsubmittedDialog = false"
            >关闭</el-button
          >
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { ref, reactive, onMounted, computed } from "vue";
import { ElMessage } from "element-plus"; // 如果需要使用 Element Plus 的消息提示
import route from "@/router";
import { homework } from "@/api/teacher/homework.js";
import { useUserInfoStore } from "@/store/user.js";
//获取用户状态
const userInfoStore = useUserInfoStore();

// 批改 状态
const opt = ref({
  0: { label: "已批改", color: "green", icon: "✅" }, // 已批改状态
  1: { label: "未批改", color: "red", icon: "❌" }, // 未批改状态
});

// 章节名称
const z = ref("Mysql");
// 小节名称
const x = ref("索引");

// 数据内容  每个人
const taskList = ref([]);

// 获取当前时间 日期选择器
const selectedDate = ref(new Date().toISOString().split("T")[0]);

// 控制弹框显示与隐藏的状态变量
const showUnsubmittedDialog = ref(false);

// 计算属性，用于获取未提交作业的学生列表
const unsubmittedStudents01 = computed(() => {
  return Array.isArray(taskList.value)
    ? taskList.value.filter((task) => task.commit === null)
    : [];
});

// 计算属性，用于过滤掉未提交作业的学生列表
const filteredTaskList = computed(() => {
  return Array.isArray(taskList.value)
    ? taskList.value.filter((task) => task.commit === 0)
    : [];
});
// 总人数
const totalStudents = computed(() =>
  Array.isArray(taskList.value) ? taskList.value.length : 0
);

// 已交作业的人数
const submittedStudents = computed(() => {
  return Array.isArray(taskList.value)
    ? taskList.value.filter((task) => task.commit === 0).length
    : 0;
});

// 未交作业的人数
const unsubmittedStudents = computed(() => {
  return Array.isArray(taskList.value)
    ? taskList.value.filter((task) => task.commit === null).length
    : 0;
});

const taskUser = ref({
  userId: userInfoStore.userInfo.userId, // 用户ID
  submitTime: selectedDate.value, // 提交时间
});

const home = async (taskUser) => {
  let shuju = await homework(taskUser.value);
  taskList.value = shuju.data;
  console.log(shuju);
};

onMounted(() => {
  // 调用接口
  home(taskUser);
});

// 批改作业的方法
const correct = (userId) => {
  // 实现批改作业的逻辑
  route.push("/class/CorrectingWork?userId="+userId);
};
</script>

<style lang="scss" scoped>
/* 添加一些样式以美化日期选择器 */
input[type="date"] {
  padding: 8px;
  font-size: 16px;
  border: 1px solid #ccc;
  border-radius: 4px;
}
.common-layout-header {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.common-layout-header-title {
  font-size: 20px;
  display: flex;
  font-family: "微软雅黑";
  align-items: center;
}

.common-layout-header-title img {
  width: 30px;
  display: flex;
}

.common-layout-header-button {
  display: flex;
  align-items: center;
  gap: 10px;
}
input[type="date"] {
  margin-left: 10px;
  padding: 5px;
  border: 1px solid #ccc;
  border-radius: 4px;
}
/* homework.vue 或者全局样式文件 */
.homework-header {
  margin-top: 20px;
}

.date-input {
  margin-bottom: 10px;
}

.class-info {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
}

.student-stats {
  display: flex;
  gap: 10px;
}

.clickable {
  cursor: pointer;
  color: #409eff;
  text-decoration: underline;
}

.clickable:hover {
  color: #66b1ff;
}
.custom-dialog {
  border-radius: 10px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.el-table {
  margin-top: 20px;
}

.el-table th {
  background-color: #f5f7fa;
}

.el-table td {
  padding: 12px 0;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
}
</style>
